package com.rambo.xpd.oslib.demo;

import com.rambo.xpd.base.vo.StudentGridlb;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Create by Rambo on 2017/5/24
 **/
public class Dom4jDemo {

    public static void main(String[] args) throws Exception {
        String path = Thread.currentThread().getContextClassLoader().getResource("demo.xml").getPath();

        SAXReader reader = new SAXReader();
        Document document = reader.read(new File(path));

        List<StudentGridlb> studentGridlbList = new ArrayList<>();
        StudentGridlb studentGridlbVo;
        for (Object classGridlb : document.getRootElement().elements("classGridlb")) {
            Element classGridlbEle = (Element) classGridlb;

            for (Object studentGridlb : classGridlbEle.element("studentGrid").elements("studentGridlb")) {
                Element studentGridlbEle = (Element) studentGridlb;

                //这里可以使用 Xstream 来序列号业务 PO
                XStream xstream = new XStream(new DomDriver());
                xstream.processAnnotations(StudentGridlb.class);
                studentGridlbList.add((StudentGridlb) xstream.fromXML(studentGridlbEle.asXML()));

              /*  studentGridlbVo = new StudentGridlb();
                studentGridlbVo.setStu_id(studentGridlbEle.elementTextTrim("stu_id"));
                studentGridlbVo.setStu_age(Integer.parseInt(studentGridlbEle.elementTextTrim("stu_age")));
                studentGridlbVo.setStu_name(studentGridlbEle.elementTextTrim("stu_name"));
                DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                studentGridlbVo.setStu_birthday(format.parse(studentGridlbEle.elementTextTrim("stu_birthday")));
                studentGridlbList.add(studentGridlbVo);*/
            }
        }
        System.out.println(studentGridlbList.toString());
    }
}